package com.raising.modules.sys.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.SysConfig;
import com.raising.modules.sys.service.SysConfigService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.modules.sys.utils.SysConfigUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 公共配置表 控制器
 * @author gaoy
 * @createTime 2018-01-17 11:30:52
 */
@Controller
@RequestMapping("${adminPath}/sys/sysConfig")
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
    * 跳转至列表页面
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        olog("进入公共配置管理页面");
        return "sys/sysConfig/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Model model, SysConfig sysConfig,Page<SysConfig> page) throws Exception{
        page.setEntity(sysConfig);
        page.setOrderBy(" a.id asc ");
        return ResultVo.success().data(sysConfigService.getPage(page));
    }

    /**
    * 跳转新增页面
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute("sysConfig", new SysConfig());
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/sysConfig/add";
    }

    /**
    * 执行新增操作
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(SysConfig sysConfig, RedirectAttributes redirectAttributes) throws Exception{
        sysConfigService.save(sysConfig);
        addMessage(redirectAttributes,"新增成功");
        // 清空缓存
        SysConfigUtil.removeCache();
        olog("新增公共配置项["+sysConfig.getConfigKey()+"]",sysConfig.toString());
        return "redirect:"+ adminPath +"/sys/sysConfig/update?id="+sysConfig.getId();
    }

    /**
    * 跳转更新页面
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model) throws Exception{
        SysConfig sysConfig = sysConfigService.get(id);
        model.addAttribute("sysConfig", sysConfig);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/sysConfig/edit";
    }
    /**
    * 执行更新操作
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(SysConfig sysConfig, RedirectAttributes redirectAttributes) throws Exception{
        sysConfigService.save(sysConfig);
        addMessage(redirectAttributes,"修改成功");
        // 清除缓存
        SysConfigUtil.removeCache();
        olog("编辑公共配置项["+sysConfig.getConfigKey()+"]",sysConfig.toString());
        return "redirect:"+ adminPath +"/sys/sysConfig/update?id="+sysConfig.getId();
    }
    /**
    * 执行删除操作
    * @author gaoy
    * @createTime 2018-01-17 11:30:52
    */
    @RequiresPermissions("sys:sysConfig:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) throws Exception{
        SysConfig entity = sysConfigService.get(id);
        sysConfigService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        // 清除缓存
        SysConfigUtil.removeCache();
        olog("删除公共配置项["+entity.getConfigKey()+"]",entity.toString());
        return "redirect:"+ adminPath +"/sys/sysConfig/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
